import axios from 'axios'
import Element from 'element-ui'
import router from './router'
import store from './store'

axios.defaults.baseURL = "http://localhost:8081"
//axios.defaults.baseURL = "http://121.41.230.131:8081"
// 前置拦截
axios.interceptors.request.use(config => {
  return config
})

// 后置拦截
axios.interceptors.response.use(
  response => {
    let res = response.data;
    //console.log("=============")
   // console.log(res)
   // console.log("=============")

    if (res.code === 200) {
      return response
    } else {
      //console("code!= 200")
      Element.Message({
        showClose: true,
        message: res.message,
        type: 'error'
      });
      return Promise.reject(res.message)
    }

  },
  error => {

    if (error.response.code === 401) {
      store.commit("REMOVE_INFO")
      router.push("/login")
    }

    error.message = error.response.data.message
    Element.Message({
      showClose: true,
      message: error.message,
      type: 'error'
    });
    return Promise.reject(error.message)
  }
)
